package com.jdd.modules.deviceConf.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jdd.common.util.LoginUserUtils;
import com.jdd.common.util.oConvertUtils;
import com.jdd.modules.device.entity.TDevice;
import com.jdd.dao.entity.TProject;
import com.jdd.modules.device.mapper.TDeviceMapper;
import com.jdd.dao.mapper.TProjectMapper;
import com.jdd.modules.deviceConf.entity.TDeviceConf;
import com.jdd.modules.deviceConf.mapper.TDeviceConfMapper;
import com.jdd.modules.deviceConf.service.TDeviceConfService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.modules.deviceConf.vo.TDeviceConfTreeModal;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 设备配置表 服务实现类
 * </p>
 *
 * @author zly
 * @since 2022-03-24
 */
@Service
public class TDeviceConfServiceImpl extends ServiceImpl<TDeviceConfMapper, TDeviceConf> implements TDeviceConfService {

    @Resource
    private TDeviceMapper tDeviceMapper;

    @Resource
    private TProjectMapper tProjectMapper;

    @Override
    public List<TDeviceConfTreeModal> queryTreeList() {

        QueryWrapper<TDevice> queryWrapper = new QueryWrapper<>();
        QueryWrapper<TProject> wrapper = new QueryWrapper<>();
        List<String> projectCodeList = LoginUserUtils.getProjectCodeList();
        queryWrapper.in("project_id", projectCodeList);
        List<TDevice> devices = tDeviceMapper.selectList(queryWrapper);
        wrapper.in("id",projectCodeList);
        List<TProject>  tProjects =tProjectMapper.selectList(wrapper);
        List<TDeviceConfTreeModal> listResult =  wrapTreeDataToTreeList(devices,tProjects);
        return listResult;
    }

    private List<TDeviceConfTreeModal> wrapTreeDataToTreeList(List<TDevice> devices, List<TProject> tProjects) {
        List<TDeviceConfTreeModal> records = new ArrayList<>();
        for (int i = 0; i < devices.size(); i++) {
            TDevice tDevice = devices.get(i);
            records.add(new TDeviceConfTreeModal(tDevice));
        }
        for (int i = 0; i < tProjects.size(); i++) {
            TProject tProject = tProjects.get(i);
            records.add(new TDeviceConfTreeModal(tProject));
        }
        List<TDeviceConfTreeModal> tree = findChidren(records);
        setEmptyChildrenAsNull(tree);
        return tree;
    }

    private List<TDeviceConfTreeModal> findChidren(List<TDeviceConfTreeModal> records) {
        List<TDeviceConfTreeModal> treeList = new ArrayList<>();
        for (int i = 0; i < records.size(); i++) {
            TDeviceConfTreeModal branch = records.get(i);
            if (oConvertUtils.isEmpty(branch.getParentId())) {
                treeList.add(branch);
            }
        }
        getGrandChildren(treeList,records);
        return treeList;
    }

    private static void getGrandChildren(List<TDeviceConfTreeModal> treeList,List<TDeviceConfTreeModal> recordList) {
        for (int i = 0; i < treeList.size(); i++) {
            TDeviceConfTreeModal model = treeList.get(i);
            for (int i1 = 0; i1 < recordList.size(); i1++) {
                TDeviceConfTreeModal m = recordList.get(i1);
                if (m.getParentId()!=null&&m.getParentId().equals(model.getId())) {
                    model.getChildren().add(m);
                }
            }
            getGrandChildren(treeList.get(i).getChildren(), recordList);
        }

    }

    private static void setEmptyChildrenAsNull(List<TDeviceConfTreeModal> treeList) {

        for (int i = 0; i < treeList.size(); i++) {
            TDeviceConfTreeModal model = treeList.get(i);
            if (model.getChildren().size() == 0) {
                model.setChildren(null);
                model.setLeaf(true);
            }else{
                setEmptyChildrenAsNull(model.getChildren());
                model.setLeaf(false);
            }
        }
    }


}
